/**
 * Created with JetBrains PhpStorm.
 * User: Evan Hou
 * Date: 12-8-12
 * Time: 上午12:39
 * To change this template use File | Settings | File Templates.
 */
var user = {};

//检查账号类型 return boolean; true passport, false nickname
user.checkAccountType = function (v)
{
    v = parseInt(v);

    var type = (v) ? wq.getRadioCheckBoxValue('account_type') : $('#account_type').val();
    type = parseInt(type);

    return type ? true : false;
}

//检查用户名称
user.checkUserName = function (v)
{
    var userName = document.getElementById('username_id').value;
    var accountType = user.checkAccountType(v);

    $('#username_notice_id').html('输入正确');

    if (accountType) {
        if ( !wq.isEmpty(userName) ) {
            $('#username_notice_id').html('<span style="color:red;">用户名为空。</span>');
            return false;
        }

        if ( !wq.limit_length(userName) ) {
            $('#username_notice_id').html('<span style="color:red;">用户名小于6或大于32个字符。</span>');
            return false;
        }

//        if ( !wq.isEmail(userName) ) {
//            $('#username_notice_id').html('<span style="color:red;">用户名必须为邮件地址。</span>');
//            return false;
//        }

    } else {
        var userName = user.checkNickName();
        if ( !userName ) {
            return false;
        }
    }

    return userName;
}

user.isExist = function ()
{
    var userName = document.getElementById('username_id').value;
    var type = wq.getRadioCheckBoxValue('account_type');

    var data = wq.ajax('/user/register/userIsExist', 'passport='+userName+'&type='+(type) ? '1' : '0');

    var prompt = '';
    switch ( data.error ) {
        case '0': prompt = '<span style="color: green;">验证成功</span>'; break;
        case '10010': prompt = '<span style="color: red;">用户名为空</span>'; break;
        case '10002': prompt = '<span style="color: red;">用户已存在</span>'; break;
        default : prompt = '<span style="color: red;">系统繁忙，请稍后再试</span>'; break;
    }

    $('#username_notice_id').html(prompt);

    return (data.error == '0') ? userName : false;
}

user.isActivation = function ()
{
    var userName = document.getElementById('username_id').value;
    var type = wq.getRadioCheckBoxValue('account_type');
    type = (type) ? '1' : '0';
    var _return = false;
    wq.jsonp("user/register/isActivation", {'passport':userName, 'type':type},function(data){
        //console.log(data.error);return;
        var prompt = '';
        switch ( data.error ) {
            case '0': prompt = '<span style="color: green;">验证成功</span>'; break;
            case '10010': prompt = '<span style="color: red;">用户名为空</span>'; break;
            case '10006': prompt = '<span style="color: red;">用户不存在</span>'; break;
            default : prompt = '<span style="color: red;">系统繁忙，请稍后再试</span>'; break;
        }

        $('#username_notice_id').html(prompt);

        _return = (data.error == '0') ? userName : false;
    }, false);
    return _return;



    /*
    var data = wq.ajax('/user/register/isActivation', '&passport='+userName+'&type='+type);

    if ( !wq.isEmpty(data.error) ) {
        data.error = '';
    }
    //*/
}

//检查用户昵称
user.checkNickName = function ()
{
    var nickName = document.getElementById('username_id').value;

    if ( !wq.isEmpty(nickName) ) {
        $('#username_notice_id').html('<span style="color:red;">昵称为空。</span>');
        return false;
    }

    if ( !wq.limit_length(nickName) ) {
        $('#username_notice_id').html('<span style="color:red;">昵称小于6或大于32个字符。</span>');
        return false;
    }

    return nickName;
}

//检查密码
user.checkPassword = function ()
{
    var passWord = document.getElementById('password_id').value;

    $('#password_notice_id').html('输入正确');

    if ( !wq.isEmpty(passWord) ) {
        $('#password_notice_id').html('<span style="color:red;">密码为空。</span>');
        return false;
    }

    if ( !wq.limit_length(passWord) ) {
        $('#password_notice_id').html('<span style="color:red;">密码小于6或大于32个字符。</span>');
        return false;
    }

    return passWord;
}

//检查重复密码
user.checkRePassword = function ()
{
    var passWord = user.checkPassword();
    var rePassWord = document.getElementById('repassword_id').value;

    if ( !passWord ) return false;

    $('#repassword_notice_id').html('输入正确');

    if ( !wq.isEmpty (rePassWord) ) {
        $('#repassword_notice_id').html('<span style="color:red;">确认密码为空！</span>');
        return false;
    }

    if ( !wq.limit_length(rePassWord, 6, 32) ) {
        $('#repassword_notice_id').html('<span style="color:red;">确认密码小于6位或大于32位。</span>');
        return false;
    }

    if (passWord != rePassWord) {
        $('#repassword_notice_id').html('<span style="color:red;">两个输入密码不一致！</span>');
        return false;
    }

    return rePassWord;
}

//用户提交登陆
user.submitLogin = function (e)
{
    if (e.keyCode == 13) {
        user.userLogin();
    }
}

//用户登陆
user.userLogin = function ()
{
    var source = document.getElementById('source_id').value;
    source = parseInt(source);

    var userName = user.checkUserName();
    if ( !userName ) {
        return false;
    }

    var passWord = user.checkPassword();
    if ( !passWord ) {
        return false;
    }

    var param = 'username='+userName+'&password='+passWord+'&source='+source+'&remember=1';
    var data = wq.ajax('/user/login/submit', param);
}

user.modifyPassword = function ()
{
    var oldPassword = document.getElementById('old_password_id').value;

    $('#old_password_notice_id').html('输入正确');

    if ( !wq.isEmpty(oldPassword) ) {
        $('#old_password_notice_id').html('<span style="color:red;">原密码为空。</span>');
        return false;
    }

    if ( !wq.limit_length(oldPassword) ) {
        $('#old_password_notice_id').html('<span style="color:red;">原密码格式错误。</span>');
        return false;
    }

    var password = user.checkPassword();
    if ( !password ) return;
    if ( !user.checkRePassword() ) return;

    var data = wq.ajax('/user/center/submitModifyPassword', 'old_password='+oldPassword+'&new_password='+password);

    var prompt = '系统繁忙，请稍后再试!';
    switch ( data.error ) {
        case '0': prompt = '修改成功'; break;
        case '10012': prompt = '参数不全'; break;
        case '10009': prompt = '您未登陆'; break;
        case '10006': prompt = '用户不存在'; break;
        case '10007': prompt = '原密码输入错误'; break;
        case '10013': prompt = '系统繁忙，请稍后再试'; break;
    }

    alert(prompt);
    if (data.error == '0') {
        wq.goToUrl('/user/login');
    }
}

user.modifyProfile = function ()
{
    var idCard = document.getElementById('id_card').value;
    var sex = wq.getRadioCheckBoxValue('sex');
    sex = sex ? sex : '';
    var birthday = document.getElementById('birthday').value;
    var address = $("#address").find("option:selected").text();;//document.getElementById('address').value;
    var phone = document.getElementById('phone').value;
    var email = document.getElementById('email').value;

    if ( wq.isEmpty(phone) && !wq.isMobile(phone)) {
        alert('手机号码格式错误');
        return false;
    }

    if ( wq.isEmpty(email) && !wq.isEmail(email)) {
        alert('邮箱格式错误');
        return false;
    }

    var param = 'id_card='+idCard+'&sex='+sex+'&birthday='+birthday+'&address='+address+'&phone='+phone+'&email='+email;
    var data = wq.ajax('user/center/saveProfile', param);

    if (data.error == '0') {
        alert('修改成功');
        wq.goToUrl('/user/center/profile');
    } else {
        alert('修改失败');
    }
}

//用户比赛报名
user.gameApply = function ()
{
    wq.jsonp("game/gameApply", {},function(data){
        if (data.error == '0') {
            alert('报名成功');
            wq.reload();
            return ;
        }

        var prompt = '系统繁忙，请稍后再试';
        switch (data.error) {
            case '10009': prompt = '您还没有登陆'; break;
            case '10015': prompt = '您还没有登陆过客户端'; break;
            case '10016': prompt = '您的段位还不到7段'; break;
            case '10017': prompt = '您已报名'; break;
            case '99999': prompt = '系统繁忙，请稍后再试'; break;
        }

        alert(prompt);
    });


    /*
    var data = wq.ajax('game/gameApply', '');

    if (data.error == '0') {
        alert('报名成功');
        wq.reload();
        return ;
    }

    var prompt = '系统繁忙，请稍后再试';
    switch (data.error) {
        case '10009': prompt = '您还没有登陆'; break;
        case '10015': prompt = '您还没有登陆过客户端'; break;
        case '10016': prompt = '您的段位还不到7段'; break;
        case '10017': prompt = '您已报名'; break;
        case '99999': prompt = '系统繁忙，请稍后再试'; break;
    }

    alert(prompt);
    //*/
}